//
//  RHInverterStatisticsCell3.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/6/13.
//

#import "RHInverterStatisticsCell3.h"
@interface RHInverterStatisticsCell3()

@property (nonatomic ,strong) QMUILabel *titleLab;

@property (nonatomic ,strong) UIImageView *icon;

@property (nonatomic, strong) NSMutableArray *itemsArray;

@property (nonatomic ,strong) QMUILabel *totalTitleLab;//累计

@property (nonatomic ,strong) QMUILabel *totalContentLab;
@end
@implementation RHInverterStatisticsCell3

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)setModel:(RHInverterModel *)model
{
    _model = model;
    kWeakSelf;
    self.titleLab.text = model.title.rh_localized;
    self.icon.image = [UIImage imageNamed:model.icon];
    [model.itemArr enumerateObjectsUsingBlock:^(DeviceChartFieldModel * _Nonnull fieldModel, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx<self.itemsArray.count) {
            UIView *itemView = self.itemsArray[idx];
            QMUILabel *titleLab = [itemView viewWithTag:100+idx];
            QMUILabel *contentLab = [itemView viewWithTag:200+idx];
            titleLab.text = fieldModel.keyName;
            NSDecimalNumber *value = [NSDecimalNumber decimalNumberWithString:ValidNullDataToString(ValidNullData(fieldModel.content), @"0")];
            NSString *valueStr = [NSString stringWithFormat:@"%@",value];
            contentLab.text = [NSString stringWithFormat:@"%@ (%@)",ValidNullDataToString(valueStr, @"--"),ValidNullDataToString(fieldModel.unitName, @"--")];
            NSString *key = [NSString stringWithFormat:@"(%@)",ValidNullData(fieldModel.unitName)];
            contentLab.attributedText = [NSString attributeStringWithContent:contentLab.text attriFont:kSYSTEMFONT(10) attriColor:kCOLOR_HEX_A(@"#171A1D", 0.6) attriKey:key];
        }
        
        
        //最后一个累计量是加在bgview上的
//        if (idx == weakSelf.itemsArray.count) {
//            self.totalTitleLab.text = ValidNullDataToString(fieldModel.keyName, @"--");
//            NSString *value = [NSDecimalNumber decimalPlacesWithoutRoundDownWithDigit:3 value:[fieldModel.content doubleValue]];
//            self.totalContentLab.text = [NSString stringWithFormat:@"%@%@",ValidNullDataToString(value, @"--"),ValidNullDataToString(fieldModel.unitName, @"--")];
//        }
    }];
   
}


- (void)setupUI{
    self.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];
    
    UIImageView *iconImg = [[UIImageView alloc]init];
    [bgView addSubview:iconImg];
    self.icon = iconImg;
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(25);
        make.left.top.mas_offset(15);
    }];
    
    QMUILabel *titleLab = [QMUILabel labelWithText:@"--" font:kBOLDSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#3F4443")];
    titleLab.textAlignment = NSTextAlignmentLeft;
    titleLab.numberOfLines = 2;
    titleLab.adjustsFontSizeToFitWidth = YES;
    titleLab.minimumScaleFactor = 0.8;
    [bgView addSubview:titleLab];
    self.titleLab = titleLab;
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImg.mas_right).offset(10);
        make.centerY.equalTo(iconImg);
        make.width.offset(150);
    }];
    
    
    
    
    UIView *itemBgView = kView;
    [bgView addSubview:itemBgView];
    [itemBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(iconImg.mas_bottom).offset(5);
        make.bottom.mas_offset(-5);
    }];
    
    CGFloat bgWidth = MIN(kUI_WINDOW_W, kUI_WINDOW_H) - 30;
    __block UIView *lastview;
    NSArray *titles = @[@"--",@"--",@"--",@"--"];
    CGFloat itemWidth = bgWidth / 2;
    self.itemsArray = [NSMutableArray array];
    [titles enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView  = [self subItimeViewWithTitle:obj index:idx];
        [itemBgView addSubview:itemView];
        [self.itemsArray addObject:itemView];
        if (idx == 0) {
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(itemWidth);
                make.left.mas_offset(0);
                make.top.mas_offset(0);
                make.height.offset(60);
            }];
        }else{
            
            if (idx % 2) {
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.size.equalTo(lastview);
                    make.left.equalTo(lastview.mas_right);
                    make.top.equalTo(lastview);
                }];
            }else{
                //换行
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.size.equalTo(lastview);
                    make.left.mas_offset(0);
                    make.top.equalTo(lastview.mas_bottom);
                }];
            }
            
        }
        
        //小竖线
        if (idx %2 == 0) {
            UIView *line = kView;
            line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
            [itemView addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(1);
                make.height.offset(35);
                make.centerY.mas_offset(0);
                make.right.mas_offset(0);
            }];
        }
        
        //大横线
        if (idx == 1) {
            UIView *line = kView;
            line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
            [bgView addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.offset(1);
                make.left.mas_offset(35);
                make.right.mas_offset(-35);
                make.top.equalTo(itemView.mas_bottom);
                
            }];
        }
       
        lastview = itemView;
    }];
    //封底
    [lastview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(0);
    }];
    
    
    
    //累计发电量
    QMUILabel *totalTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX_A(@"#3F4443",0.6) textAligment:NSTextAlignmentRight];
    totalTitleLab.tag = 100+self.itemsArray.count;
    totalTitleLab.numberOfLines = 2;
    totalTitleLab.adjustsFontSizeToFitWidth = YES;
    totalTitleLab.minimumScaleFactor = 0.6;
    [bgView addSubview:totalTitleLab];
    
    QMUILabel *totalContentLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#3F4443") textAligment:NSTextAlignmentCenter];
    totalContentLab.tag = 200+self.itemsArray.count;
    [bgView addSubview:totalContentLab];
    
    
    [totalContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.centerY.equalTo(iconImg);
    }];
    
    [totalTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(totalContentLab.mas_left).offset(-5);
        make.centerY.equalTo(totalContentLab);
        make.width.offset(90);
    }];
    
    self.totalTitleLab = totalTitleLab;
    self.totalContentLab = totalContentLab;
}




-(UIView*)subItimeViewWithTitle:(NSString *)title index:(NSInteger)idx;
{
    UIView *itemView = kView;
    QMUILabel *subTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#3F4443",0.6) textAligment:NSTextAlignmentCenter];
    subTitleLab.text = title.rh_localized;
    subTitleLab.tag = 100+idx;
    subTitleLab.numberOfLines = 2;
    subTitleLab.adjustsFontSizeToFitWidth = YES;
    subTitleLab.minimumScaleFactor = 0.8;
    [itemView addSubview:subTitleLab];
    
    QMUILabel *contentLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#3F4443") textAligment:NSTextAlignmentCenter];
    contentLab.tag = 200+idx;
    [itemView addSubview:contentLab];
    
    [subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(3);
        make.right.mas_offset(-3);
        make.top.equalTo(itemView.mas_centerY).offset(3);
    }];
    
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(3);
        make.right.mas_offset(-3);
        make.bottom.equalTo(itemView.mas_centerY).offset(-3);
    }];
    return itemView;
}

@end
